package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;

import com.flqy.baselibrary.utils.T;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.common.SafeConsumer;
import com.shenzhen.horselive.common.utils.Permissions;
import com.shenzhen.horselive.databinding.ActivityFeedbackBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.utils.A;
import com.shenzhen.horselive.widget.ZzImageBox;

import java.io.File;
import java.util.List;

import gun0912.tedbottompicker.PhotoPicker;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class FeedbackActivity extends AppBarActivity {

    ActivityFeedbackBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityFeedbackBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("BUG反馈");
        initViews();
    }
    private void initViews() {
        binding.zzImageBox.setOnImageClickListener(new ZzImageBox.AbsOnImageClickListener() {
            @Override
            public void onImageClick(int position, String url, String realPath, int realType, ImageView iv, String tag) {

            }

            @Override
            public void onAddClick() {
                super.onAddClick();
                chooseImage();
            }

            @Override
            public void onDeleteClick(int position, String url, String realPath, int realType, String tag) {
                super.onDeleteClick(position, url, realPath, realType, tag);
                binding.zzImageBox.removeImage(position);
            }
        });
        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });



    }

    //提交信息
    private void submit() {
        String  content =  binding.limitInput.getText();
        if (TextUtils.isEmpty(content))
        {
            showToast("请填写反馈内容");
            return;
        }

        MultipartBody.Builder  builder  = new MultipartBody.Builder()
                .addFormDataPart("token", User.get().getMyInfo().getToken())
                .addFormDataPart("content",content);
       if (binding.zzImageBox.getAllImages()!=null)
        for (int i = 0; i <binding.zzImageBox.getAllImages().size(); i++) {
            File file = new File(binding.zzImageBox.getAllImages().get(i));
            RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("files", file.getName(), fileBody);
        }

        ApiFactory.getApiService().feedback(builder.build())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            showToast("提交成功");
                            finish();
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });



    }

    private void chooseImage() {
        Permissions.of(this).request(getString(R.string.take_photo_get_sdcard_error),
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE).subscribe(new SafeConsumer<Boolean>() {
            @Override
            public void accept(Boolean granted) {
                if (granted) {
                    //权限申请成功
                    new PhotoPicker.Builder(FeedbackActivity.this).setSelectMaxCount(1)
                            .setOnImageSelectedListener(new PhotoPicker.OnImageSelectedListener() {
                                @Override
                                public void onImageSelected(Pair<Uri, String> imagePath) {
                                    binding.zzImageBox.addImage(imagePath.second);

                                }
                            }).create().show();
                } else {
                    //权限申请失败
                    T.showShort("权限申请失败");
                }
            }
        });

    }
}